import {
  list as designerList
} from '../../../../api/designer'
import {
  list
} from '../../../../api/product'

import {
  list as tagList
} from '../../../../api/tag'
const util = require('../../../../utils/util')
let app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    info: {
      type: Object
    }
  },
  lifetimes: {
    attached() {
      if(this.data.info.rows!=''||this.data.info.rows!=null) {
        this.data.pageSize = this.data.info.rows
        this.setData({
          pageSize:this.data.pageSize
        })
      }
      var _this = this
      if (this.data.info.tagids != null) {
        this.data.tagIds = this.data.info.tagids.split(',')
        for (var i = 0; i < this.data.tagIds.length; i++) {
          this.data.tagIds[i] = parseInt(this.data.tagIds[i])
        }
      }
      this.data.canvasH = (wx.getSystemInfoSync().windowWidth / 750).toFixed(2)
      this.setData({
        tagIds: this.data.tagIds
      })
      this.loadTags()
      //       this.loadProduct()
    }

  },
  /**
   * 组件的初始数据
   */
  data: {
    tagIds: [],
    tagId: '',
    scrollTop: '',
    tags: [],
    allHeight: 'height:600rpx',
    length: '',
    pageId: 18,
    componentsList: [],
    productList: [],
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
    pageNum: 1,
    pageSize: 20,
    productType: false,
    catchtouchmove: true,
    canvasH: null,
    screenHeight:null
  },

  attached() {
    var that = this;

    /**
     * 获取当前设备的宽高
     */
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
  },
  created() {
  },

  /**
   * 组件的方法列表
   */
  methods: {

    loadTags() {
      var _this = this;
      tagList({
        pageNum: this.data.pageNum,
        pageSize: 5,
        mchId: app.globalData.mchId,
        type: '3',
        tagIds: this.data.tagIds,
      }).then(res => {
        _this.data.tags = res.data.data
        _this.data.tagId = this.data.tags[0].id
        _this.setData({
          tagId: _this.data.tagId,
          tags: _this.data.tags
        })

        // setTimeout(() => {
        _this.data.pageNum = 0
        _this.data.productList = []
        _this.loadProduct()
        // }, 1000)


      })

    },
    //  tab切换逻辑
    swichNav: function (e) {
      this.data.pageNum = 0
      this.data.tagId = e.currentTarget.dataset.id
      this.data.productList = []
      this.loadProduct()
      var that = this;
      if (this.data.currentTab === e.target.dataset.current) {
        return false;
      } else {
        that.setData({
          tagId: this.data.tagId,
          currentTab: e.target.dataset.current
        })
      }
    },

    loadProduct(callback) {
      var _this = this
      _this.data.productType = false
      _this.data.pageNum += 1
      var allHeight = 0
      list({
        tagIds: [this.data.tagId],
        mchId: app.globalData.mchId,
        pageSize: _this.data.pageSize,
        pageNum: _this.data.pageNum,
      }).then(res => {
        if (_this.data.pageNum == 1) {
          _this.data.productList = []
        }
        res.data.data.forEach(item => {
          item.thumbnail = util.thumbnail(item.thumbnail, 360, 360)
          _this.data.productList.push(item)
        })
        if (res.data.data.length < 20) {
          _this.data.productType = true
        }
        _this.setData({
          productList: _this.data.productList,
          pageNum: _this.data.pageNum,
          productType: _this.data.productType
        })
        if (callback) {
          callback()
        }
      })
    },
    bindChange: function (e) {

      this.data.tagId = this.data.tags[e.detail.current].id
      this.data.pageNum = 0
      this.data.productList = []
      this.loadProduct()
      this.setData({
        currentTab: e.detail.current
      });

    },
    //+++++++++++上拉加载+++++++++
    onReachBottom: function (callback) {
      this.loadProduct(() => {
        if (callback) {
          callback()
        }
      })

    },
    downRefresh: function () {
      this.data.productList = []
      this.data.pageSize = 20
      this.data.pageNum = 0
      this.loadProduct()
      this.setData({
        productList: this.data.productList,
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize
      })
    },
  }
})
